package vcp.unittests;


import static org.junit.Assert.*;

import java.awt.List;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import client.Message;
import db.parking.beans.*;
import db.tables.*;


import run.ServerFrame;
import server.SimpleServer;
import server.SimpleServer.InvalidException;


public class SubscriberCheck extends TestCase {

	private ServerFrame server;
	private List liste = new List();
	private Orders o1;
	private Subscriber s1;
	private SimpleServer simple;
	private Message msg = null;


	public  SubscriberCheck(String name) {
		super(name);
	}

	public  SubscriberCheck() {
		// TODO Auto-generated constructor stub
	}

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		server= new ServerFrame(12345);
		server.setDbUser("root");
		server.setDbPass("31387650");
		server.setDbHost("localhost");
		server.setDbName("vcp");
		server.setDbPort("3306");
		
		server.listen();
		msg = new Message();
		AdminCustomer.update("2", 0); //init amount to 0
	}

	@After
	public void tearDown() throws Exception {
	}



	
	@Test
	public void test1() throws Exception {
		
		long time = System.currentTimeMillis();// Set current time at this second to StartTime
		Timestamp currTimestamp = new Timestamp(time);
		Timestamp leaveTime = new Timestamp(time);
		Timestamp subscriptionStart = new Timestamp(time);
		subscriptionStart.setHours(currTimestamp.getHours() -2);
		leaveTime.setHours(currTimestamp.getHours() + 10);

		
		//calculate Payment for full subscriber that is using lot 3	
		s1 = new Subscriber("3", "3",subscriptionStart, "Full", 3, leaveTime, null,null, 0);
		//get Occasional customer rate from Lot 3 and multiply it by 72
		rates r = AdminRates.getRow(3, 2);
		float expected2 = r.getAmount() * 72;	
		msg.setObj(s1);
		
		float result2 = server.getServer().calculateSubscriberPayment(s1);

		Assert.assertEquals(expected2,result2);	
		
	}
	
	
	public void test2() throws Exception {
		
		//Test3 - Check that we receive exception on unknown customer type
		long time = System.currentTimeMillis();// Set current time at this second to StartTime
		Timestamp currTimestamp = new Timestamp(time);
		Timestamp leaveTime = new Timestamp(time);
		Timestamp subscriptionStart = new Timestamp(time);
		subscriptionStart.setHours(currTimestamp.getHours() -2);
		leaveTime.setHours(currTimestamp.getHours() + 10);
		
		String expected = "Unknown Customer Type";
		
		s1 = new Subscriber("3", "3",subscriptionStart, "Unknown", 3, leaveTime, null,null, 0);

		try{
		
			 server.getServer().calculateSubscriberPayment(s1);	
		}
		catch(InvalidException e)
		{
			Assert.assertEquals(expected,e.getMessage());	
		}
		
	}
	
	
	public void test3() throws Exception {
		
		//Test3 - Check that currentTime is after Subscription start
		String expected = "Error:Subscription hasn't started yet";
		
		long time = System.currentTimeMillis();// Set current time at this second to StartTime
		Timestamp currTimestamp = new Timestamp(time);
		Timestamp leaveTime = new Timestamp(time);
		Timestamp subscriptionStart = new Timestamp(time);
		subscriptionStart.setHours(currTimestamp.getHours() +2);
		leaveTime.setHours(currTimestamp.getHours() + 10);
		
		s1 = new Subscriber("3", "3",subscriptionStart, "Full", 3, leaveTime, null,null, 0);

		try{
		
			 server.getServer().calculateSubscriberPayment(s1);	
		}
		catch(InvalidException e)
		{
			Assert.assertEquals(expected,e.getMessage());	
		}
		
	}
	
	public void test4() throws Exception {
		
		//Test4 - Check Null object
		String expected = "Object is Null";
		
		s1 = null;

		try{
		
			 server.getServer().calculateSubscriberPayment(s1);	
		}
		catch(InvalidException e)
		{
			Assert.assertEquals(expected,e.getMessage());	
		}
		
	}
	
	public void test5() throws Exception {
		
		//Test4 - Check that we dont get a price amount for a subcriber that is more then 28 days 
		//after subscription start.
		
		String expected = "Please Renew Your Subscription";
		long time = System.currentTimeMillis();// Set current time at this second to StartTime
		Timestamp currTimestamp = new Timestamp(time);
		Timestamp leaveTime = new Timestamp(time);
		Timestamp subscriptionStart = new Timestamp(time);
		subscriptionStart.setMonth(currTimestamp.getMonth()-2);
		leaveTime.setHours(currTimestamp.getHours() + 10);
		
		s1 = new Subscriber("3", "3",subscriptionStart, "Full", 3, leaveTime, null,null, 0);	

		try{
		
			 server.getServer().calculateSubscriberPayment(s1);	
		}
		catch(InvalidException e)
		{
			Assert.assertEquals(expected,e.getMessage());	
		}
		
	}
	
	

	

}





